﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class FormCxgz : Form
    {
        public FormCxgz()
        {
            InitializeComponent();
        }
        


        private void button刷新_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("007001001"))
            {
                List金额();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
           
        }

        private void List金额()
        {
            string sql;
            ///最大顺序号的报工单，未入库的
            //sql = @"    select ''行号,z_baogong.生产订单号 生产订单号,Z_UA_User.cuser_id 报工人员工号,Z_UA_User.cuser_name 报工人员,isnull(报工数量,0) 报工数量,isnull(工序单价,0) 工序单价
            //    ,isnull(报工数量*工序单价,0) 计件金额,isnull(报工工时,0) 报工工时,isnull(工时单价,0) 工时单价
            //    ,isnull(报工工时*工时单价,0) 工时金额,sum(isnull(结算金额,0)) 结算金额  from z_baogong
            //     join Z_UA_User  on Z_UA_User.cUSER_Id=z_baogong.报工人员  

            //     where 1=1 and 是否已经计算='已结算'  ";
            sql = @"select ''行号,z_baogong.生产订单号 生产订单号,物料编号,Inventory.cInvName 物料名称,工序名称,Z_UA_User.cuser_id 报工人员工号,Z_UA_User.cuser_name 报工人员
                    ,isnull(报工数量,0) 报工数量,报工时间,isnull(工序单价,0) 工序单价
                    ,isnull(报工数量*工序单价,0) 计件金额,sum(isnull(结算金额,0)) 结算金额  from z_baogong
                    join Z_UA_User  on Z_UA_User.cUSER_Id=z_baogong.报工人员
                    join Inventory on 物料编号 = Inventory.cInvCode  where 1=1 and 是否已经计算='已结算' ";

            if (this.comboBox报工人员.Text != "请选择")
            {
                sql += " and Z_UA_User.cuser_name ='" + this.comboBox报工人员.Text + "'";
            }
            if (this.textBox1.Text != "")
            {
                sql += " and Z_UA_User.cuser_id like '%" + this.textBox1.Text + "%'";
            }
            if (this.textBox2.Text != "")
            {
                sql += " and  生产订单号 like '%" + this.textBox2.Text + "%'";
            }

            if (this.dateTimePicker_ksrq.Checked)
            {
                sql += " and 报工时间>='" + this.dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql += " and 报工时间<'" + this.dateTimePicker_jsrq.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            }


            //sql += " group by z_baogong.生产订单号,Z_UA_User.cuser_name,报工数量,工序单价,报工数量*工序单价,报工工时,工时单价,报工工时*工时单价,Z_UA_User.cuser_id ";
            sql += @"group by z_baogong.生产订单号,Z_UA_User.cuser_name,报工数量,工序单价,报工数量*工序单价,Z_UA_User.cuser_id,物料编号,Inventory.cInvName,工序名称,报工时间";
            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView1.AutoGenerateColumns = false;

            decimal 合计;
            合计 = 0;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["行号"] = (i + 1).ToString();
                    合计 += ToNum(tb.Rows[i]["结算金额"].ToString());
                }
            }

            DataRow dr2 = tb.NewRow();

            dr2["结算金额"] = Math.Round(合计, 2);
            tb.Rows.InsertAt(dr2, 0);

            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();
        }

        internal static decimal ToNum(string p)
        {
            try
            {
                return decimal.Parse(p);
            }
            catch
            {
                return 0;
            }
        }

        private void FormCxgz_Load(object sender, EventArgs e)
        {
            comboBox报工人员Initl();
        }

        private void comboBox报工人员Initl()
        {
            string sql;
            sql = "select  cUser_Name from Z_UA_User where  bCCUser is not  null and cdept !='系统组' order by cUser_Name ";

            DataTable tb = pub.SysGetTable(sql);
            DataRow dr = tb.NewRow();
            dr["cUser_Name"] = "请选择";
            tb.Rows.InsertAt(dr, 0);
            comboBox报工人员.DataSource = tb;

            comboBox报工人员.DisplayMember = "cUser_Name";
        }


      
      

      

        private void button2_Click(object sender, EventArgs e)
        {
            //if (pub.是否有权限("007001002"))
            //{
            //    string fileName = "";
            //    string saveFileName = "";
            //    SaveFileDialog saveDialog = new SaveFileDialog();
            //    saveDialog.DefaultExt = "xls";
            //    saveDialog.Filter = "Excel文件|*.xls";
            //    saveDialog.FileName = fileName;
            //    saveDialog.ShowDialog();
            //    saveFileName = saveDialog.FileName;
            //    if (saveFileName.IndexOf(":") < 0) return; //被点了取消
            //    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            //    if (xlApp == null)
            //    {
            //        MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");
            //        return;
            //    }
            //    Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            //    Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            //    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
            //    //写入标题             
            //    for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //    { worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }
            //    //写入数值
            //    for (int r = 0; r < dataGridView1.Rows.Count; r++)
            //    {
            //        for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //        {
            //            worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
            //        }
            //        System.Windows.Forms.Application.DoEvents();
            //    }
            //    worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            //    MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);
            //    if (saveFileName != "")
            //    {
            //        try
            //        {
            //            workbook.Saved = true;
            //            workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 
            //        }
            //        catch (Exception ex)
            //        {//fileSaved = false;                      
            //            MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);
            //        }
            //    }
            //    xlApp.Quit();
            //    GC.Collect();//强行销毁           }
            //}
            //else
            //{
            //    MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            //}
        }
    }
}
